﻿using LCL.CodeHelper;
using LCL.IBuilder;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.Builder.IBatisNet
{
    public partial class ProjectBuilder
    {
        #region BuilderDAL
        private void BuilderDAL()
        {
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                #region MyRegion
                var Fieldlist = DbObject.GetColumnInfoList(DbName, table.TableName);
                List<TableColumn> Keys = Fieldlist.Where(p => p.IsPrimaryKey == true).ToList();
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                string ModelSpace = NameSpace + ".Model." + className;//命名空间 + 类名
                string DALSpace = NameSpace + ".DAL." + className;
                string BLLSpace = NameSpace + ".BLL." + className;

                #region StringBuilder
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("using System.Collections.Generic; ");
                builder.AppendLine("using System.Data; ");
                builder.AppendLine("using " + NameSpace + ".IDAL; ");
                builder.AppendLine(" ");
                builder.AppendLine("namespace "+NameSpace+".DAL ");
                builder.AppendLine("{ ");
                builder.AppendLine("    /// <summary>  ");
                builder.AppendLine("    /// " + table.ToString());
                builder.AppendLine("    /// </summary>  ");
                builder.AppendLine("    public partial class " + className + " : BatisDAO, I" + className + "DAL ");
                builder.AppendLine("    { ");
                builder.AppendLine("        public bool Exists(" + CodeCommon.GetInParameter(Keys) + ") ");
                builder.AppendLine("        { ");
                builder.AppendLine("            int ret = 0; ");
                builder.AppendLine("            return ret > 0; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 增加一条数据  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public int Add(" + ModelSpace + " model) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            int pk = (int)base.Insert(\"" + className + "_Insert\", model); ");
                builder.AppendLine("            return pk; ");
                builder.AppendLine("        } ");
                builder.AppendLine(" ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 更新一条数据  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public bool Update(" + ModelSpace + " model) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            int cnt = base.Update(\"" + className + "_Update\", model); ");
                builder.AppendLine("            return cnt>0; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 删除一条数据  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public bool Delete(" + CodeCommon.GetInParameter(Keys) + ") ");
                builder.AppendLine("        { ");
                builder.AppendLine("            " + ModelSpace + " model = this.GetModel(" + CodeCommon.GetInParameter(Keys) + ");");
                builder.AppendLine("            int cnt = base.Delete(\"" + className + "_Delete\", model); ");
                builder.AppendLine("            return cnt>0;			 ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 得到一个对象实体  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public " + ModelSpace + " GetModel(" + CodeCommon.GetInParameter(Keys) + ") ");
                builder.AppendLine("        { ");
                builder.AppendLine("            " + ModelSpace + " obj = base.SelectOne(\"" + className + "_Select\", " + CodeCommon.GetSeleteColumn(Keys, "", false) + ") as " + ModelSpace + "; ");
                builder.AppendLine("            return obj;		 ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 获得数据列表  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public DataTable GetList(string strWhere) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            return null; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 获得数据列表  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public List<" + ModelSpace + "> GetListModel(string strWhere) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            List<" + ModelSpace + "> list = (List<" + ModelSpace + ">)base.Select(\"" + className + "_Select\", strWhere); ");
                builder.AppendLine("            return list; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        /// <summary>  ");
                builder.AppendLine("        /// 得到一个对象实体  ");
                builder.AppendLine("        /// </summary>  ");
                builder.AppendLine("        public " + ModelSpace + " DataRowToModel(DataRow row) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            return null; ");
                builder.AppendLine("        } ");
                builder.AppendLine("    } ");
                builder.AppendLine("} ");
                builder.AppendLine(" ");

                #endregion
                string filename = CodeCommon.DesktopDirectory + NameSpace + @"/DAL/" + className + ".cs";
                CodeCommon.CreateFiles(filename, builder.ToString());
                #endregion
            }
            BaseDAO();
            DALCsproj();
        }
        private void DALCsproj()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
            builder.AppendLine("<Project DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" ToolsVersion=\"4.0\"> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <ProjectType>Local</ProjectType> ");
            builder.AppendLine("    <ProductVersion>8.0.50727</ProductVersion> ");
            builder.AppendLine("    <SchemaVersion>2.0</SchemaVersion> ");
            builder.AppendLine("    <ProjectGuid>{" + DALGuid + "}</ProjectGuid> ");
            builder.AppendLine("    <Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration> ");
            builder.AppendLine("    <Platform Condition=\" '$(Platform)' == '' \">AnyCPU</Platform> ");
            builder.AppendLine("    <ApplicationIcon> ");
            builder.AppendLine("    </ApplicationIcon> ");
            builder.AppendLine("    <AssemblyKeyContainerName> ");
            builder.AppendLine("    </AssemblyKeyContainerName> ");
            builder.AppendLine("    <AssemblyName>" + NameSpace + ".DAL</AssemblyName> ");
            builder.AppendLine("    <AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    </AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    <DefaultClientScript>JScript</DefaultClientScript> ");
            builder.AppendLine("    <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout> ");
            builder.AppendLine("    <DefaultTargetSchema>IE50</DefaultTargetSchema> ");
            builder.AppendLine("    <DelaySign>false</DelaySign> ");
            builder.AppendLine("    <OutputType>Library</OutputType> ");
            builder.AppendLine("    <RootNamespace>" + NameSpace + ".DAL</RootNamespace> ");
            builder.AppendLine("    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> ");
            builder.AppendLine("    <StartupObject> ");
            builder.AppendLine("    </StartupObject> ");
            builder.AppendLine("    <FileUpgradeFlags> ");
            builder.AppendLine("    </FileUpgradeFlags> ");
            builder.AppendLine("    <UpgradeBackupLocation> ");
            builder.AppendLine("    </UpgradeBackupLocation> ");
            builder.AppendLine("    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> ");
            builder.AppendLine("    <OldToolsVersion>2.0</OldToolsVersion> ");
            builder.AppendLine("    <PublishUrl>publish\\</PublishUrl> ");
            builder.AppendLine("    <Install>true</Install> ");
            builder.AppendLine("    <InstallFrom>Disk</InstallFrom> ");
            builder.AppendLine("    <UpdateEnabled>false</UpdateEnabled> ");
            builder.AppendLine("    <UpdateMode>Foreground</UpdateMode> ");
            builder.AppendLine("    <UpdateInterval>7</UpdateInterval> ");
            builder.AppendLine("    <UpdateIntervalUnits>Days</UpdateIntervalUnits> ");
            builder.AppendLine("    <UpdatePeriodically>false</UpdatePeriodically> ");
            builder.AppendLine("    <UpdateRequired>false</UpdateRequired> ");
            builder.AppendLine("    <MapFileExtensions>true</MapFileExtensions> ");
            builder.AppendLine("    <ApplicationRevision>0</ApplicationRevision> ");
            builder.AppendLine("    <ApplicationVersion>1.0.0.%2a</ApplicationVersion> ");
            builder.AppendLine("    <IsWebBootstrapper>false</IsWebBootstrapper> ");
            builder.AppendLine("    <UseApplicationTrust>false</UseApplicationTrust> ");
            builder.AppendLine("    <BootstrapperEnabled>true</BootstrapperEnabled> ");
            builder.AppendLine("    <TargetFrameworkProfile /> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Debug\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>DEBUG;TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>true</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>false</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>full</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Release\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>false</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>true</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>none</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <Reference Include=\"LCL.Common\"> ");
            builder.AppendLine("      <HintPath>..\\Lib\\LCL.Common.dll</HintPath> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System\"> ");
            builder.AppendLine("      <Name>System</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Data\"> ");
            builder.AppendLine("      <Name>System.Data</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Drawing\"> ");
            builder.AppendLine("      <Name>System.Drawing</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Xml\"> ");
            builder.AppendLine("      <Name>System.XML</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <Compile Include=\"BatisDB.cs\" /> ");
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                builder.AppendLine("    <Compile Include=\"" + className + ".cs\" /> ");
            }
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Client.3.5\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName> ");
            builder.AppendLine("      <Install>false</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Framework.3.5.SP1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Windows.Installer.3.1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>Windows Installer 3.1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <ProjectReference Include=\"..\\IDAL\\" + NameSpace + ".IDAL.csproj\"> ");
            builder.AppendLine("      <Project>{" + IDALGuid + "}</Project> ");
            builder.AppendLine("      <Name>" + NameSpace + ".IDAL</Name> ");
            builder.AppendLine("    </ProjectReference> ");
            builder.AppendLine("    <ProjectReference Include=\"..\\Model\\" + NameSpace + ".Model.csproj\"> ");
            builder.AppendLine("      <Project>{" + ModelGuid + "}</Project> ");
            builder.AppendLine("      <Name>" + NameSpace + ".Model</Name> ");
            builder.AppendLine("    </ProjectReference> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <Import Project=\"$(MSBuildBinPath)\\Microsoft.CSharp.targets\" /> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <PreBuildEvent> ");
            builder.AppendLine("    </PreBuildEvent> ");
            builder.AppendLine("    <PostBuildEvent> ");
            builder.AppendLine("    </PostBuildEvent> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("</Project> ");
            string filename = CodeCommon.DesktopDirectory + NameSpace + @"/DAL/" + NameSpace + ".DAL.csproj";
            CodeCommon.CreateFiles(filename, builder.ToString());
        }
        #endregion

        private void BaseDAO()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("using System; ");
            builder.AppendLine("using System.Collections; ");
            builder.AppendLine("using System.Data; ");
            builder.AppendLine("using System.Data.Common; ");
            builder.AppendLine("using IBatisNet.Common; ");
            builder.AppendLine("using IBatisNet.DataAccess; ");
            builder.AppendLine("using IBatisNet.DataAccess.DaoSessionHandlers; ");
            builder.AppendLine("using IBatisNet.DataAccess.Exceptions; ");
            builder.AppendLine("using IBatisNet.DataMapper; ");
            builder.AppendLine("using IBatisNet.DataMapper.Configuration.Statements; ");
            builder.AppendLine("using IBatisNet.DataMapper.MappedStatements; ");
            builder.AppendLine("using IBatisNet.DataMapper.Scope; ");
            builder.AppendLine(" ");
            builder.AppendLine("namespace "+NameSpace+".DAL    ");
            builder.AppendLine("{ ");
            builder.AppendLine("    /// <summary> ");
            builder.AppendLine("    /// 接口名称：IBaseDAO ");
            builder.AppendLine("    /// 内容摘要：DAO的基本接口 ");
            builder.AppendLine(" ");
            builder.AppendLine("    /// </summary> ");
            builder.AppendLine("    public interface IBaseDAO : IBatisNet.DataAccess.Interfaces.IDao ");
            builder.AppendLine("    { ");
            builder.AppendLine("        #region 自动生成代码 ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>结果集合</returns> ");
            builder.AppendLine("        IList Select(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>单个结果</returns> ");
            builder.AppendLine("        Object SelectOne(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Update语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>更新的行数</returns> ");
            builder.AppendLine("        int Update(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通DeleteDeelte语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>删除的行数</returns> ");
            builder.AppendLine("        int Delete(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Insert语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>插入行的PK对象</returns> ");
            builder.AppendLine("        object Insert(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用得到参数化后的SQL(map文件中参数要使用$标记) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns></returns> ");
            builder.AppendLine("        string GetSql(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        DataTable GetDataTable(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用的以DataTable的方式得到skip Select的结果(xml文件中参数要使用$标记的占位参数) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <param name=\"pageIndex\">页码</param> ");
            builder.AppendLine("        /// <param name=\"pageSize\">每页显示数</param> ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        DataTable GetDataTable(string tag, object paramObject, int pageIndex, int pageSize); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行skip Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <param name=\"skipResults\">忽略个数</param> ");
            builder.AppendLine("        /// <param name=\"maxResults\">最大个数</param> ");
            builder.AppendLine("        /// <returns>结果集合</returns> ");
            builder.AppendLine("        IList Select(string tag, object paramObject, int skipResults, int maxResults); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 执行存储过程 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>单个结果</returns> ");
            builder.AppendLine("        object ExecuteProcedure(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 返回Sql影响的记录总数 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param>  ");
            builder.AppendLine("        /// <returns>Sql影响的记录总数</returns> ");
            builder.AppendLine("        int GetCount(string tag, object paramObject); ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 返回Sql影响的记录总数 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"sql\">Sql</param> ");
            builder.AppendLine("        /// <returns>Sql影响的记录总数</returns> ");
            builder.AppendLine("        int GetCount(string sql); ");
            builder.AppendLine("        #endregion ");
            builder.AppendLine("    } ");
            builder.AppendLine("    /// <summary> ");
            builder.AppendLine("    /// Batis实现的DAO基类 ");
            builder.AppendLine("    /// </summary> ");
            builder.AppendLine("    public abstract class BatisDAO : IBaseDAO ");
            builder.AppendLine("    { ");
            builder.AppendLine("        private DaoSession m_DaoSession = null; ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 得到DaoSession ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <returns>当前的DaoSession</returns> ");
            builder.AppendLine("        protected DaoSession GetContext() ");
            builder.AppendLine("        { ");
            builder.AppendLine("            IDaoManager daoManager = DaoManager.GetInstance(this); ");
            builder.AppendLine("            m_DaoSession = (daoManager.LocalDaoSession as DaoSession); ");
            builder.AppendLine("            if (m_DaoSession != null) ");
            builder.AppendLine("            { ");
            builder.AppendLine("                SqlMapDaoSession session = m_DaoSession as SqlMapDaoSession; ");
            builder.AppendLine(" ");
            builder.AppendLine("                if (session != null) ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    session.SqlMap.SessionStore = new IBatisNet.DataMapper.SessionStore.HybridWebThreadSessionStore(session.SqlMap.Id); ");
            builder.AppendLine("                } ");
            builder.AppendLine("            } ");
            builder.AppendLine("            return m_DaoSession; ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 取得SqlMap ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        protected ISqlMapper SqlMap ");
            builder.AppendLine("        { ");
            builder.AppendLine("            get ");
            builder.AppendLine("            { ");
            builder.AppendLine("                SqlMapDaoSession session = GetContext() as SqlMapDaoSession; ");
            builder.AppendLine(" ");
            builder.AppendLine("                if (session != null) ");
            builder.AppendLine("                    return session.SqlMap; ");
            builder.AppendLine("                return null; ");
            builder.AppendLine("            } ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>结果集合</returns> ");
            builder.AppendLine("        public IList Select(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            string strSQL = SqlMap.MappedStatements.ToString(); ");
            builder.AppendLine("            return SqlMap.QueryForList(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行skip Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <param name=\"skipResults\">忽略个数</param> ");
            builder.AppendLine("        /// <param name=\"maxResults\">最大个数</param> ");
            builder.AppendLine("        /// <returns>结果集合</returns> ");
            builder.AppendLine("        public IList Select(string tag, object paramObject, int skipResults, int maxResults) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.QueryForList(tag, paramObject, skipResults, maxResults); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Select语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>单个结果</returns> ");
            builder.AppendLine("        public Object SelectOne(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.QueryForObject(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Update语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>更新的行数</returns> ");
            builder.AppendLine("        public int Update(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.Update(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Deelte语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>删除的行数</returns> ");
            builder.AppendLine("        public int Delete(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.Delete(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用执行Insert语句 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>插入行的PK对象</returns> ");
            builder.AppendLine("        public object Insert(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.Insert(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 执行存储过程 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>单个结果</returns> ");
            builder.AppendLine("        public object ExecuteProcedure(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            return SqlMap.QueryForObject(tag, paramObject); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        #region GetSql/GetDataTable ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用得到参数化后的SQL(xml文件中参数要使用$标记的占位参数) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>获得的SQL</returns> ");
            builder.AppendLine("        public string GetSql(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            IMappedStatement mappedStatement = SqlMap.GetMappedStatement(tag); ");
            builder.AppendLine("            IStatement statement = mappedStatement.Statement; ");
            builder.AppendLine("            RequestScope request = statement.Sql.GetRequestScope(mappedStatement, paramObject, ");
            builder.AppendLine("                       new SqlMapSession(SqlMap)); ");
            builder.AppendLine("            return request.PreparedStatement.PreparedSql; ");
            builder.AppendLine("        } ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        public DataTable GetDataTable(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            try ");
            builder.AppendLine("            { ");
            builder.AppendLine("                DataSet ds = new DataSet(); ");
            builder.AppendLine("                bool isSessionLocal = false; ");
            builder.AppendLine("                IDalSession session = SqlMap.LocalSession; ");
            builder.AppendLine("                if (session == null) ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    session = new SqlMapSession(SqlMap); ");
            builder.AppendLine("                    session.OpenConnection(); ");
            builder.AppendLine("                    isSessionLocal = true; ");
            builder.AppendLine("                } ");
            builder.AppendLine("                IDbCommand cmd = session.CreateCommand(CommandType.Text); ");
            builder.AppendLine("                cmd.CommandText = GetSql(tag, paramObject); ");
            builder.AppendLine("                try ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    cmd.Connection = session.Connection; ");
            builder.AppendLine("                    IDbDataAdapter adapter = session.CreateDataAdapter(cmd); ");
            builder.AppendLine("                    adapter.Fill(ds); ");
            builder.AppendLine("                } ");
            builder.AppendLine("                finally ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    if (isSessionLocal) ");
            builder.AppendLine("                    { ");
            builder.AppendLine("                        session.CloseConnection(); ");
            builder.AppendLine("                    } ");
            builder.AppendLine("                } ");
            builder.AppendLine(" ");
            builder.AppendLine("                return ds.Tables[0]; ");
            builder.AppendLine(" ");
            builder.AppendLine("            } ");
            builder.AppendLine("            catch (Exception ex) ");
            builder.AppendLine("            { ");
            builder.AppendLine("                throw new DataAccessException(\"Can't GetDataTable, tag = \" + tag, ex); ");
            builder.AppendLine("            } ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 通用的以DataTable的方式得到skip Select的结果(xml文件中参数要使用$标记的占位参数) ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <param name=\"PageIndex\">页码</param> ");
            builder.AppendLine("        /// <param name=\"PageSize\">每页显示数</param> ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        public DataTable GetDataTable(string tag, object paramObject, int PageIndex, int PageSize) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            try ");
            builder.AppendLine("            { ");
            builder.AppendLine("                int skipResults = PageIndex * PageSize; ");
            builder.AppendLine("                int maxResults = (PageIndex + 1) * PageSize; ");
            builder.AppendLine("                string sql = GetPageSQL(tag, paramObject, skipResults, maxResults); ");
            builder.AppendLine("                IDbCommand cmd = SqlMap.LocalSession.CreateCommand(CommandType.Text); ");
            builder.AppendLine("                cmd.CommandText = sql; ");
            builder.AppendLine(" ");
            builder.AppendLine("                DbDataAdapter adapter = SqlMap.LocalSession.CreateDataAdapter(cmd) as DbDataAdapter; ");
            builder.AppendLine("                if (adapter == null) ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    throw new NotSupportedException(\"Not support skip GetDataTable\"); ");
            builder.AppendLine("                } ");
            builder.AppendLine(" ");
            builder.AppendLine("                DataTable dt = new DataTable(); ");
            builder.AppendLine("                adapter.Fill(dt); ");
            builder.AppendLine(" ");
            builder.AppendLine("                return dt; ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine("            } ");
            builder.AppendLine("            catch (Exception ex) ");
            builder.AppendLine("            { ");
            builder.AppendLine("                throw new DataAccessException(\"Can't skip GetDataTable, tag = \" + tag, ex); ");
            builder.AppendLine("            } ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 返回Sql影响的记录总数 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param>  ");
            builder.AppendLine("        /// <returns>Sql影响的记录总数</returns> ");
            builder.AppendLine("        public int GetCount(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            string sql = this.GetSql(tag, paramObject); ");
            builder.AppendLine("            return GetCount(sql); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 返回Sql影响的记录总数 ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"sql\">Sql</param> ");
            builder.AppendLine("        /// <returns>Sql影响的记录总数</returns> ");
            builder.AppendLine("        public int GetCount(string sql) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            try ");
            builder.AppendLine("            { ");
            builder.AppendLine("                sql = GetTotalSQL(sql); ");
            builder.AppendLine("                IDbCommand cmd = SqlMap.LocalSession.CreateCommand(CommandType.Text); ");
            builder.AppendLine("                cmd.CommandText = sql; ");
            builder.AppendLine(" ");
            builder.AppendLine("                // 修改记录01 开始 ");
            builder.AppendLine("                // 判断链接是否是关闭状态，是关闭则开启 ");
            builder.AppendLine("                if (cmd.Connection.State == ConnectionState.Closed) ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    cmd.Connection.Open(); ");
            builder.AppendLine("                } ");
            builder.AppendLine("                int iTotalCount = Convert.ToInt32(cmd.ExecuteScalar()); ");
            builder.AppendLine(" ");
            builder.AppendLine("                // 判断链接是否是开启状态，是关闭则关闭 ");
            builder.AppendLine("                if (cmd.Connection.State == ConnectionState.Open) ");
            builder.AppendLine("                { ");
            builder.AppendLine("                    cmd.Connection.Close(); ");
            builder.AppendLine("                } ");
            builder.AppendLine("                // 修改记录01 结束 ");
            builder.AppendLine("                return iTotalCount; ");
            builder.AppendLine("            } ");
            builder.AppendLine("            catch (Exception ex) ");
            builder.AppendLine("            { ");
            builder.AppendLine("                throw new DataAccessException(\"Can't skip GetCount, sql = \" + sql); ");
            builder.AppendLine("            } ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 生成分页的Sql语句 ，支持Oracle ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param>  ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        private string GetTotalSQL(string tag, object paramObject) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            string sql = this.GetSql(tag, paramObject); ");
            builder.AppendLine("            return GetTotalSQL(sql); ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 生成分页的Sql语句 ，支持Oracle ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"sql\">语句ID</param>         ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        private string GetTotalSQL(string sql) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            string PageSqlFormat = @\"select  count(1) from ({0}) Oracle_Page_Table1 \"; ");
            builder.AppendLine("            string PageSql = string.Format(PageSqlFormat, sql); ");
            builder.AppendLine("            return PageSql; ");
            builder.AppendLine("        } ");
            builder.AppendLine("        /// <summary> ");
            builder.AppendLine("        /// 生成分页的Sql语句 ，支持Oracle ");
            builder.AppendLine("        /// </summary> ");
            builder.AppendLine("        /// <param name=\"tag\">语句ID</param> ");
            builder.AppendLine("        /// <param name=\"paramObject\">语句所需要的参数</param> ");
            builder.AppendLine("        /// <param name=\"skipResults\">忽略个数</param> ");
            builder.AppendLine("        /// <param name=\"maxResults\">最大个数</param> ");
            builder.AppendLine("        /// <returns>得到的DataTable</returns> ");
            builder.AppendLine("        private string GetPageSQL(string tag, object paramObject, int skipResults, int maxResults) ");
            builder.AppendLine("        { ");
            builder.AppendLine("            string sql = this.GetSql(tag, paramObject); ");
            builder.AppendLine("            string PageSqlFormat = @\" ");
            builder.AppendLine("                select Oracle_Page_Table2.* from  ");
            builder.AppendLine("                ( ");
            builder.AppendLine("                select rownum as Oracle_Page_Rownum_Id ,Oracle_Page_Table1.* from ({0})   Oracle_Page_Table1 where rownum <= {2} ");
            builder.AppendLine("                )   Oracle_Page_Table2 ");
            builder.AppendLine("                where Oracle_Page_Table2.Oracle_Page_Rownum_Id > {1} ");
            builder.AppendLine("                \"; ");
            builder.AppendLine("            string PageSql = string.Format(PageSqlFormat, sql, skipResults, maxResults); ");
            builder.AppendLine("            return PageSql; ");
            builder.AppendLine("        } ");
            builder.AppendLine(" ");
            builder.AppendLine("        #endregion ");
            builder.AppendLine("    } ");
            builder.AppendLine("} ");


            string filename = CodeCommon.DesktopDirectory + NameSpace + @"/DAL/BatisDB.cs";
            CodeCommon.CreateFiles(filename, builder.ToString());

            try
            {
                string TargetDirectory = CodeCommon.DesktopDirectory + NameSpace + @"/DAL";
                VSProject.AddClass2012(TargetDirectory + @"/" + NameSpace + ".DAL.csproj", @"BatisDB.cs");
            }
            catch { }
        }
    }
}
